package com.example.demo.scheduledtask;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.example.demo.common.Constant;
import com.example.demo.config.ErpConfig;
import com.example.demo.mapper.PoMpsinfoMapper;
import com.example.demo.mapper.WmsPurchaseMapper;
import com.example.demo.utils.SoapXmlUtil;
import com.example.demo.vo.SalesReturnsListVo;
import com.example.demo.vo.SalesReturnsVo;
import com.example.demo.vo.XmlResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author : Zhang.yt
 * @date : 2024/7/29
 */
@Component
@Slf4j
@DS("wms")
public class SalesReturns {

    @Resource
    private WmsPurchaseMapper wmsPurchaseMapper;

    @Autowired
    private SoapXmlUtil soapXmlUtil;

    @Autowired
    PoMpsinfoMapper poMpsinfoMapper;

    @Resource
    private ErpConfig erpConfig;

    /**
     * 销售退货入库
     */
    @Scheduled(fixedDelay = 26 * 1000)//开启定时同步
    //@Async(value = "taskExecutor")
    public void SalesReturns() {
        SalesReturnsVo salesReturns = wmsPurchaseMapper.getSalesReturns();
        if (ObjectUtil.isEmpty(salesReturns) || ObjectUtil.isEmpty(salesReturns.getXmdkdocno())) {
            log.info("未查询到销售退货入库过账数据，修改报错数据重新过账");
            wmsPurchaseMapper.SalesReturnsLose();
            return;
        }

        //捕获异常，设置单据状态为失败，避免总是取到该单据导致后续任务无法处理
        try {
            salesReturns.setXmdksite(Constant.PMDSSITE_AMA);
            salesReturns.setActionType(Constant.ActionType_I);
            //查询明细表
            String xmdkdocno = salesReturns.getXmdkdocno();
            List<SalesReturnsListVo> salesReturnsList = wmsPurchaseMapper.getSalesReturnsList(xmdkdocno);
            String resMessage = soapXmlUtil.getSoapXml("SalesReturnPost", "xmdk_t", "xmdl_t", salesReturns, salesReturnsList);
            //转换为soap消息
            log.info("soap接口销售退货:{}", resMessage);
            //状态占用
            wmsPurchaseMapper.updateSalesReturnsCallback(salesReturns.getXmdkdocno(), "erp待返回", 3);
            //发送soap消息，并解析结果
            String result;
            try {
                result = soapXmlUtil.sendSoapMessageNew(resMessage, erpConfig.getUrl());
            } catch (Exception e) {
                result = soapXmlUtil.sendSoapMessageNew(resMessage, erpConfig.URL2);
            }
            //调用等待
            if (salesReturnsList.size() >= 50) {
                Thread.sleep(50 * 1000);
            } else if (salesReturnsList.size() >= 30) {
                Thread.sleep(20 * 1000);
            } else if (salesReturnsList.size() >= 10) {
                Thread.sleep(5 * 1000);
            }
            log.info("soap接口销售退货回调->{}", result);
            XmlResp xmlResp = soapXmlUtil.analysisXml(result);
            if (xmlResp.getCode().equals("0")) {
                //更新相应表状态为已下发
                wmsPurchaseMapper.updateSalesReturnsCallback(salesReturns.getXmdkdocno(), xmlResp.getData().get("xmdkdocno"), 1);
            } else {
                //更新相应表状态为已下发
                wmsPurchaseMapper.updateSalesReturnsCallback(salesReturns.getXmdkdocno(), xmlResp.getMessage(), 2);
            }
        } catch (Exception e) {
            wmsPurchaseMapper.updateSalesReturnsCallback(salesReturns.getXmdkdocno(), "非常见的数据处理问题，请联系开发人员处理", 2);
            e.printStackTrace();
        }
    }
}
